package com.atguigu.utils;


import com.atguigu.entity.SkuUser;
import com.atguigu.entity.SkuView;
import com.atguigu.service.SkuUserService;
import com.atguigu.service.SkuViewService;
import com.atguigu.util.DayUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.Set;


/**
 * ScheduledTask
 *
 * @author yangchao
 * @version 开发阶段
 * @description TODO
 * @date 2022/3/7 9:37
 */
@Component
public class ScheduledTask {
    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuViewService skuViewService;
    @Autowired
    private SkuUserService skuUserService;
    private final static  String  HOT_KEY="Commodity-HotScore";
    private final static  String  USER_VIEWS="USER-VIEWS";


    @Async
    @Scheduled(cron = "0 0,30 * * * ? ")  //间隔30分钟
    public void first() throws InterruptedException {

       Set<String> range = redisTemplate.opsForZSet().range(HOT_KEY, 0, -1);
       range.forEach(e->{
           Long score =new Double(redisTemplate.opsForZSet().score(HOT_KEY,e)).longValue() ;
           String projectIdstr = e.substring(e.indexOf(":") + 1);
           Long projectId=Long.valueOf(projectIdstr).longValue();
           Long sumViewData= skuViewService.sumViewData(projectId);
           sumViewData = sumViewData==null?0:sumViewData;
           Date date = null;
           try {
                date = DayUtils.getSdf().parse(DayUtils.getSdf().format(new Date()));
           } catch (ParseException parseException) {
               parseException.printStackTrace();
           }

           QueryWrapper<SkuView> wrapper = new QueryWrapper<>();
           wrapper.eq("product_id",projectId);
           wrapper.eq("view_date",date);
           SkuView serviceOne = skuViewService.getOne(wrapper);
           if(serviceOne==null){
               SkuView skuView = new SkuView();
               skuView.setViewDate(date);
               skuView.setCreateTime(new Date());
               skuView.setViewNum(score-sumViewData);
               skuView.setProductId(projectId);
               skuViewService.save(skuView);
           }
           else{
               serviceOne.setCreateTime(new Date());
//               Long sumViewDataNotoday= skuViewService.sumViewDataNotoday(projectId,DayUtils.getSdf().format(new Date()));
               serviceOne.setViewNum((score-sumViewData+serviceOne.getViewNum()));
               skuViewService.updateById(serviceOne);
           }

       });
    }
    @Async
    @Scheduled(cron = "0 0/1 * * * ? ")  //间隔30分钟
    public void second() throws InterruptedException {

        Set<String> range = redisTemplate.opsForZSet().range(USER_VIEWS, 0, -1);
        range.forEach(e->{
            Long score =new Double(redisTemplate.opsForZSet().score(USER_VIEWS,e)).longValue() ;
            String projectIdstr = e.substring(e.indexOf(":") + 1);
            Long userId=Long.valueOf(projectIdstr).longValue();
            Long sumViewData= skuUserService.sumViewData(userId);
            sumViewData = sumViewData==null?0:sumViewData;
            Date date = null;
            try {
                date = DayUtils.getSdf().parse(DayUtils.getSdf().format(new Date()));
            } catch (ParseException parseException) {
                parseException.printStackTrace();
            }

            QueryWrapper<SkuUser> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userId);
            wrapper.eq("view_date",date);
            SkuUser serviceOne = skuUserService.getOne(wrapper);
            if(serviceOne==null){
                SkuUser skuUser = new SkuUser();
                skuUser.setViewDate(date);
                skuUser.setCreateTime(new Date());
                skuUser.setViewNum(score-sumViewData);
                skuUser.setUserId(userId);
                skuUserService.save(skuUser);
            }
            else{
                serviceOne.setCreateTime(new Date());
                serviceOne.setViewNum((score-sumViewData+serviceOne.getViewNum()));
                skuUserService.updateById(serviceOne);
            }

        });
    }


}